﻿
#region 作者和版权
/*************************************************************************************
* CLR 版本: 4.0.30319.42000
* 类 名 称: MathUtil
* 机器名称: SUNTIA-QL
* 命名空间: ServerFramework.Tools
* 文 件 名: MathUtil
* 创建时间: 2015/10/27 15:45:28
* 作 者: 乔龙 Qiaolong
* 版 权: MathUtil说明：本代码版权归乔龙所有，使用时必须带上乔龙网站地址 All Rights Reserved (C) 2015 - 2013
* 签 名: To be or not, it is not a problem !
* 网 站: http://www.suntia.net
* 邮 箱: qiaolong306@qq.com 
* 唯一标识： 0baf3727-5ee0-4922-8498-b1cbbfc8df90 
*
* 登录用户: simida
* 所 属 域: SUNTIA-QL

* 创建年份: 2015
* 修改时间:
* 修 改 人:
* 
************************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace ServerFramework.Tools
{
    public static class MathUtil
    {
        /// <summary>
        /// 是一个整数
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool IsNum(string text)
        {
            string pattern = "^\\d+$";
            Regex rx = new Regex(pattern);
            return rx.IsMatch(text);
        }

        /// <summary>
        /// 增加经验，返回：新的等级、新的经验
        /// </summary>
        /// <param name="addExp">增加的经验</param>
        /// <param name="currlv">当前等级(只适用初始等级>0的情况)</param>
        /// <param name="currExp">当前经验</param>
        /// <param name="limitLevel">限制等级(0为不限制)</param>
        /// <param name="exps">经验列表</param>
        /// <param name="newLv">新的等级</param>
        /// <param name="newExp">新的经验</param>
        public static void LevelUp(int addExp, int currlv, int currExp, int limitLevel, List<int> exps, out int newLv, out int newExp)
        {
            newLv = currlv;
            newExp = currExp;
            int maxLevel = limitLevel;
            if (limitLevel == 0 || limitLevel > exps.Count + 1)
            {
                maxLevel = exps.Count + 1;//不限制等级和经验列表数小于限制等级的情况，取经验列表数
            }
            if (maxLevel <= currlv)
            {
                return; //当前等级大于等于最大等级，不做操作
            }
            int needExp = exps[newLv - 1];
            newExp += addExp;

            while (IsLevelUp(needExp, newExp, newLv, maxLevel))
            {
                newLv++;
                newExp -= needExp;
                if (maxLevel > newLv)
                {
                    needExp = exps[newLv - 1];
                }
                else {
                    needExp = 0;
                }
            }
            newExp = Math.Min(newExp, needExp);
        }
        /// <summary>
        /// 是否升级
        /// </summary>
        /// <param name="needExp"></param>
        /// <param name="currExp"></param>
        /// <param name="currlv"></param>
        /// <param name="maxlv"></param>
        /// <returns></returns>
        private static bool IsLevelUp(int needExp, int currExp, int currlv, int maxlv)
        {
            return (currExp >= needExp && currlv < maxlv);
        }
        /// <summary>
        /// 检测两个日期， 是否在一天的某一个周期内，比如：从凌晨两点到次日凌晨两点;
        /// 注:晚上22点，offsetTime = 22
        /// </summary>
        /// <param name="aDate"></param>
        /// <param name="bDate"></param>
        /// <param name="offsetTime">时间偏移量</param>
        /// <returns></returns>
        public static bool IsSameDay(DateTime aDate, DateTime bDate, TimeSpan offsetTime)
        {

            if (aDate.Year != bDate.Year)
            {
                return false;
            }
            DateTime a = aDate - offsetTime;
            DateTime b = bDate - offsetTime;

            //if (a.Year == b.Year)
            //{
            return a.DayOfYear == b.DayOfYear;
            //}
            //else
            //{
            //    return false;
            //}

        }
        /// <summary>
        /// 检测两个日期， 是否属于连续天。
        /// </summary>
        /// <param name="aDate"></param>
        /// <param name="bDate"></param>
        /// <param name="offsetTime"></param>
        /// <returns></returns>
        public static bool IsCeaselessDay(DateTime aDate, DateTime bDate, TimeSpan offsetTime)
        {

            if (aDate.Year != bDate.Year)
            {
                return false;
            }
            else
            {
                DateTime a = aDate - offsetTime;
                DateTime b = bDate - offsetTime;
                int days = DateDiff(a, b);
                if (days == 1 || days == -1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        /// <summary>
        /// 检测2个日期相差的天数
        /// </summary>
        /// <param name="dateStart">小</param>
        /// <param name="dateEnd">大</param>
        /// <returns>相差的天数</returns>
        public static int DateDiff(DateTime dateStart, DateTime dateEnd)
        {
            DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());
            DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());

            TimeSpan sp = end.Subtract(start);

            return sp.Days;
        }

    }
}


